package nsu.edu.zsq.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.extern.slf4j.Slf4j;
import nsu.edu.zsq.bean.Student;
import nsu.edu.zsq.bean.SysUser;
import nsu.edu.zsq.bean.Teacher;
import nsu.edu.zsq.common.JsonData;
import nsu.edu.zsq.common.RequestHolder;
import nsu.edu.zsq.service.LoginService;
import nsu.edu.zsq.util.PasswordUtil;
import nsu.edu.zsq.util.UserType;

/** 
* Description: <br>
*/
@Slf4j
@Controller
public class LoginController {
    
    @Autowired
    private LoginService loginService;
    
    /**
     * 登录验证
     * @param username 账号
     * @param password 密码
     * @param userType 1：教师，2：学生，0：管理员
     * @return JsonData
     */
    @RequestMapping("/login")
    @ResponseBody
    public JsonData login(HttpServletRequest request, @RequestParam("username") String username, 
            @RequestParam("password") String password, @RequestParam("userType") String userType){
        if (username == null || username.length() <= 0) {
            return JsonData.fail("账号不能为空");
        } else if (password == null || password.length() <= 0) {
            return JsonData.fail("密码不能为空");
        } else if (UserType.TYPE_STUDENT.equals(userType)){
            // 1.学生登录
            Student student = loginService.getByStuNo(username);
            if (student == null) {
                return JsonData.fail("用户不存在");
            } else if (PasswordUtil.md5(password).equals(student.getPassword())) {
                // 登录成功
                log.info("登录成功。身份为：学生，学号为：{}",student.getStuNo());
                // 统一用户信息存放在session中的key
                request.getSession().setAttribute("user", student);
                request.getSession().setAttribute("userType", userType);
                return JsonData.success();
            } else {
                // 登录失败
                return JsonData.fail("账号或密码不正确");
            }
            
        } else if (UserType.TYPE_TEACHER.equals(userType)) {
            // 2.教师登录
            Teacher teacher = loginService.getByTeacherNo(username);
            if (teacher == null) {
                return JsonData.fail("用户不存在");
            } else if (PasswordUtil.md5(password).equals(teacher.getPassword())) {
                // 登录成功
                log.info("登录成功。身份为：教师，教师编号为：{}",teacher.getTeacherNo());
                request.getSession().setAttribute("user", teacher);
                request.getSession().setAttribute("userType", userType);
                return JsonData.success();
            } else {
                // 登录失败
                return JsonData.fail("账号或密码不正确");
            }
        } else if (UserType.TYPE_SYSUSER.equals(userType)) {
            // 3.管理员登录
            SysUser sysUser = loginService.getByAccount(username);
            if (sysUser == null) {
                return JsonData.fail("用户不存在");
            } else if (PasswordUtil.md5(password).equals(sysUser.getPassword())) {
                // 登录成功
                log.info("登录成功。身份为：管理员，用户姓名为：{}",sysUser.getUsername());
                request.getSession().setAttribute("user", sysUser);
                request.getSession().setAttribute("userType", userType);
                return JsonData.success();
            } else {
                // 登录失败
                return JsonData.fail("账号或密码不正确");
            }
        }
        return JsonData.fail("登录失败");
    }

    /**
     * 根据登录的用户类型，到达相应的首页
     * @param request
     * @return String html页面
     */
    @RequestMapping("/toIndex")
    public String toIndex(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        String userType = (String)session.getAttribute("userType");
        if (UserType.TYPE_STUDENT.equals(userType)) {
            // 1.学生登录
            model.addAttribute("student", RequestHolder.getCurrentStudent());
            return "student/student_index.html";
        } else if (UserType.TYPE_TEACHER.equals(userType)) {
            // 2.教师登录
            model.addAttribute("teacher", RequestHolder.getCurrentTeacher());
            return "teacher/teacher_index.html";
        } else if (UserType.TYPE_SYSUSER.equals(userType)) {
            // 3.管理员登录
            model.addAttribute("sysUser", RequestHolder.getCurrentSysUser());
            return "manager/manager_index.html";
        } else {
            return "redirect:/toLogin";
        }
    }
    
    /**
     * 退出登录
     * @param request
     * @return String 请求地址：/toLogin
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        // 清除session信息
        request.getSession().invalidate();
        return "login.html";
    }
    
}
